package com.example.leetcode.leetcode.hot100;

import com.example.leetcode.leetcode.tree.TreeNode;

/**
 * @author haitao
 * @date 2021/8/10 9:54 上午
 * @desc 合并二叉树，思路：先将B树对应A树节点的值累加到A树上，
 */
public class Solution617 {
    public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
        return depth(root1, root2);
    }

    public TreeNode depth(TreeNode root1, TreeNode root2) {
        if (root1 == null) {
            return root2;
        }
        if (root2 == null) {
            return root1;
        }
        TreeNode merge = new TreeNode(root1.val + root2.val);
        merge.left = depth(root1.left, root2.left);
        merge.right = depth(root1.right, root2.right);
        return merge;
    }
}
